import javax.print.DocFlavor;

public class LinkedListQueue<E> implements Queue<E>{

    private class Node<E>{
        public E e;

        public Node<E> next;

        public Node(E e,Node<E> next){
            this.e = e;
            this.next = next;
        }

        public Node(E e){
            this(e,null);
        }
        public Node(Node<E> next){
            this(null,next);
        }
        public Node(){
            this(null,null);
        }
        @Override
        public String toString(){
            return e.toString();
        }

    }

    private Node head,tail;
    private int size;

    public LinkedListQueue(){
        head = null;
        tail = null;
        size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0 ;
    }

    // 入队
    @Override
    public void enQueue(E e) {
        if (tail == null){
            // 无元素
           tail=  new Node(e);
           tail  = head;
        }else {
            // 只需要变换尾部
            tail.next   = new Node(e);
            tail = tail.next;
        }
        size++;
    }

    // 出队
    @Override
    public E dequeue() {
        if (isEmpty()){
            throw new IllegalArgumentException("队列为空");
        }
        Node<E> retNode = head;
        head = head.next;
        if (head==null){
            tail = null;
        }
        size --;
        return (E) retNode;

    }

    @Override
    public E getFront() {
        if (isEmpty()){
            throw new IllegalArgumentException("queue is Empty");
        }
        return (E) head.e;
    }


    @Override
    public String toString(){
        StringBuilder builder = new StringBuilder();
        builder.append("QUEUE front:");
        Node cur = head;
        while (cur != null){
            builder.append(cur + "->");
            cur = cur.next;
        }
        builder.append("tail");
        return builder.toString();
    }

    public static void main(String[] args) {
        LinkedListQueue<Integer> linkedListQueue = new LinkedListQueue<>();
        for (int i = 0; i < 100; i++) {
            linkedListQueue.enQueue(i);
            System.out.println(linkedListQueue);

            if (i % 3 == 2){
                linkedListQueue.dequeue();
                System.out.println(linkedListQueue);
            }

        }

    }
}
